package cn.seaboot.admin.starter.service;

import cn.seaboot.admin.mvc.cache.CacheName;
import cn.seaboot.admin.other.dao.Debug;
import cn.seaboot.admin.user.bean.User;
import cn.seaboot.commons.exception.BizException;
import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class DebugService {
    @Resource
    private Debug debug;

    @Resource
    private CacheManager cacheManager;

    public String name;

    public List<Map<String, Object>> query() {
        return debug.select("select * from t_sys_user");
    }

    public User testCache(String value) {
        return testCache2(value);
    }

    @Cacheable(cacheNames = CacheName.DEFAULT, key = "'test' + #value")
    public User testCache2(String value) {
        System.out.println("---------------------");
        User user = new User();
        user.setUsername("11111");
        Cache cache = cacheManager.getCache(CacheName.DEFAULT);
        net.sf.ehcache.Cache cache1 = (net.sf.ehcache.Cache) cache.getNativeCache();
        System.out.println(cache1.getKeys());
        return user;
    }

    @CachePut(cacheNames = CacheName.DEFAULT, key = "'test'")
    public Date putCache() {
        System.out.println("---------------------");
        return new Date();
    }

    @Transactional(rollbackFor = Exception.class)
    public int testTransactional() {
        System.out.println("================================================");
        System.out.println(this.name);
        debug.insert("INSERT INTO `temp` (`query`, `desc`) VALUES ('aaaaaaa', 'bbb')");
        debug.insert("INSERT INTO `temp` (`query`, `desc`) VALUES ('aaaaaaa', 'bbb')");
        debug.insert("INSERT INTO `temp` (`query`, `desc`) VALUES ('aaaaaaa', 'bbb')");
        debug.insert("INSERT INTO `temp` (`query`, `desc`) VALUES ('aaaaaaa', 'bbb')");
        debug.insert("INSERT INTO `temp` (`query`, `desc`) VALUES ('aaaaaaa', 'bbb')");
        debug.insert("INSERT INTO `temp` (`query`, `desc`) VALUES ('aaaaaaa', 'bbb')");
        debug.insert("INSERT INTO `temp` (`query`, `desc`) VALUES ('aaaaaaa', 'bbb')");
        debug.insert("INSERT INTO `temp` (`query`, `desc`) VALUES ('aaaaaaa', 'bbb')");
        debug.insert("INSERT INTO `temp` (`query`, `desc`) VALUES ('aaaaaaa', 'bbb')");
        debug.insert("INSERT INTO `temp` (`query`, `desc`) VALUES ('aaaaaaa', 'bbb')");
        debug.insert("INSERT INTO `temp` (`query`, `desc`) VALUES ('aaaaaaa', 'bbb')");
        debug.insert("INSERT INTO `temp` (`query`, `desc`) VALUES ('aaaaaaa', 'bbb')");
        debug.insert("INSERT INTO `temp` (`query`, `desc`) VALUES ('aaaaaaa', 'bbb')");
        debug.insert("INSERT INTO `temp` (`query`, `desc`) VALUES ('aaaaaaa', 'bbb')");
        debug.insert("INSERT INTO `temp` (`query`, `desc`) VALUES ('aaaaaaa', 'bbb')");
        debug.insert("INSERT INTO `temp` (`query`, `desc`) VALUES ('aaaaaaa', 'bbb')");
        debug.insert("INSERT INTO `temp` (`query`, `desc`) VALUES ('aaaaaaa', 'bbb')");
        debug.insert("INSERT INTO `temp` (`query`, `desc`) VALUES ('aaaaaaa', 'bbb')");
        debug.insert("INSERT INTO `temp` (`query`, `desc`) VALUES ('aaaaaaa', 'bbb')");
        debug.insert("INSERT INTO `temp` (`query`, `desc`) VALUES ('aaaaaaa', 'bbb')");
        throw new BizException("抛出一个异常");
    }
}
